package main

import (
	"github.com/gin-gonic/gin"
	"net/http/httptest"
	"net/http"
	"io/ioutil"
	"log"
	"fmt"
	"strings"
)
func HttpGet(url string,r *gin.Engine) []byte  {
	req :=httptest.NewRequest(http.MethodGet,url,nil)
	w :=httptest.NewRecorder()
	r.ServeHTTP(w,req)
	resp :=w.Result()
	defer resp.Body.Close()
	body,err:=ioutil.ReadAll(resp.Body)
	if err !=nil {
		log.Printf("HttpGet %s Error%s",url,err.Error())
	}
	return body
}
func HttpPost(url string,v string,r *gin.Engine) []byte  {
	postBody :=strings.NewReader(v)
	log.Println(postBody)
	req,_:=http.NewRequest(http.MethodPost,url,postBody)
	req.Header.Add("Content-Type","application/x-www-form-urlencoded")
	w :=httptest.NewRecorder()
	r.ServeHTTP(w,req)
	resp :=w.Result()
	defer resp.Body.Close()
	body,err:=ioutil.ReadAll(resp.Body)
	if err!=nil {
		log.Printf("HttpPost %s Error%s",url,err.Error())
	}
	return body
}
func PrintBody(b []byte)  {
	fmt.Printf("%s\n",b)
}
func createEngine() *gin.Engine  {
	return gin.Default()
}